//
//  SystemConfigController.m
//  ios-supplier
//
//  Created by feynmanyuan on 15/4/22.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//

#import "SystemConfigController.h"
#import "AppDelegate.h"

@interface SystemConfigController ()<UIAlertViewDelegate,UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UISwitch *alertSoundSW;
- (IBAction)swAlertSound:(id)sender;
@end

@implementation SystemConfigController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
    
    [self.tableView.tableHeaderView removeFromSuperview];
    
    UIView * bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, self.tableView.bounds.size.height - 201 - TopSizeFix)];
    
    //1.初始化Button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
    //2.设置文字和文字颜色
    [button setTitle:@"退出登录" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:TextFontSizeH2];
    
    //4.设置frame
    button.frame = CGRectMake(0, bottomView.bounds.size.height - kBottomBtnContainerHeight, self.tableView.bounds.size.width, kBottomBtnContainerHeight);
    //5.设置背景色
    button.backgroundColor = ButtonBgColorImportant;
    //6.设置触发事件
    
    [bottomView addSubview:button];
    
    //省略
    //7.添加到tableView tableFooterView中
    self.tableView.tableFooterView = bottomView;
    
    [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    [self.navigationItem setLeftBarButtonItem: [self createBackView]];
}
- (UIBarButtonItem *)createBackView
{
    UIButton *leftButtonItem = [UIButton buttonWithType:UIButtonTypeCustom];
    
    leftButtonItem.frame = CGRectMake(0, 0, 11, 20);
    
    [leftButtonItem setBackgroundImage:[UIImage imageNamed:@"icon-Back"] forState:UIControlStateNormal];
    
    [leftButtonItem setImageEdgeInsets: UIEdgeInsetsMake((19 - 11) / 2, 0, (19 - 11) / 2, 0)];
    
    [leftButtonItem addTarget:self action:@selector(leftButtonItemClicked) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:leftButtonItem];
    
    return item;
}

-(void)leftButtonItemClicked
{
    [self.navigationController popViewControllerAnimated:YES];
    self.navigationController.navigationBar.hidden = YES;
}


-(void)btnClick:(id)sender {
    [JFBUIAnimation alertSure:@"确定要退出登录?" delegate:self];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if( buttonIndex == 1 ) {
        JFBGlobalSetting * setting = [JFBGlobalSetting getSetting];
        [setting logout];

//        //这里移除登录成功之后服务器返回的ut，ut是唯一的
        [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"ut"];
        [[UserInfo currentUser] clearUserInfo];
        
        UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UIViewController * loginController = (UINavigationController*)[mainStoryBoard instantiateViewControllerWithIdentifier:@"Login"];
        
        [self presentViewController:loginController animated:YES completion:^{}];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

-(void)viewDidLayoutSubviews
{
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 15;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if( indexPath.item == 1 ) {
        NSString *currentVersion = @"v2.1";
        cell.detailTextLabel.text = currentVersion;
    }
    else if( indexPath.item == 0) {
        JFBGlobalSetting * setting = [JFBGlobalSetting getSetting];
        self.alertSoundSW.on = [[setting getSettingInfo:@"alertSound"] boolValue];
        cell.hidden = YES;
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    if (indexPath.item == 3) {
        cell.hidden = YES ;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if( indexPath.row == 0 ) {
        
    } else if(indexPath.row == 1) {
//        [((AppDelegate*)[UIApplication sharedApplication].delegate) checkUpdate:true];
    }else if(indexPath.row == 2) {
        [self performSegueWithIdentifier:@"aboutMeIdentifier" sender:nil];
    } else if( indexPath.row == 3 ) {
        [self performSegueWithIdentifier:@"adviceIdentifier" sender:nil];
    }
}

- (IBAction)swAlertSound:(id)sender {
    JFBGlobalSetting * setting = [JFBGlobalSetting getSetting];
    [setting updateSettingInfo:@"alertSound" value:[NSNumber numberWithBool:self.alertSoundSW.on]];
}

@end
